// log.cpp : Defines the exported functions for the DLL application.
//

#include "stdafx.h"
#include "Logger.h"
#include <iostream>
#include <fstream>
#include <ctime> 
using namespace std;

static Logger singleton;
static bool inited(false);
static ofstream outFile;
static bool open(false);
static time_t startTime;

void Logger::writeLog(char* moduleName, char* message) {
	struct tm *tmInfo;
	time_t t = startTime + clock() / CLOCKS_PER_SEC;
	tmInfo = localtime(&t);
	if (!open) {
		char name[100];
		sprintf(name, "%d_%d_%d.log", tmInfo->tm_year + 1900, tmInfo->tm_mon+1, tmInfo->tm_mday);
		outFile.open(name, ios_base::app);
		open = true;
	}
	char hms[100];
	sprintf(hms, "%02d:%02d:%02d.%02d", tmInfo->tm_hour, tmInfo->tm_min, tmInfo->tm_sec, 
		(clock()%CLOCKS_PER_SEC) / 10);
	outFile << hms << "\t\t" << moduleName << "\t\t" << message << endl;
}

Logger::Logger() {
	if (inited)
		cout << "please use Logger.getInstance() !" << endl;
	else {
		startTime = time(NULL);
		inited = true;
	}
	return;
}

Logger Logger::getInstance() {
	return singleton;
}

Logger::~Logger() {
	outFile.close();
	open = false;
}
